<?php
	$template = 'default';
	
	$obj = new clsrscms($template);

	class clsrscms{
		//Directory which will have all the templates
		private $dirTemplate = 'templates';
		
		//Which template will be used
		private $template = 'default';
		
		//Config file which will be parsed for each template
		private $fileConfig = 'config.php';
		
		//Config file for each template which will be parsed to get data for template
		private $config = array();
		/**
		 * Capture template to be used for the site
		 */
		public function __construct($template='default') {
			$this->template = $template;
			$this->parseConfig();
			$this->execute();
		}
		/**
		 * Parse configuration file for the template
		 */
		private function parseConfig(){
			$fileConfig = $this->dirTemplate.'/'.$this->template.'/'.$this->fileConfig;
			require_once($fileConfig);
			$this->config = $config;
		}
		/**
		 * Display page not found error and exit
		 */
		private function pageNotFound(){
			echo 'Page not found';
			exit;
		}
		/**
		 * Display template based on parsed and prepared information
		 */
		private function execute(){
			$data = array();
			
			$page = filter_input(INPUT_GET,'page',FILTER_SANITIZE_STRING);
			$page = strlen(trim($page))?trim($page):$this->config['default']['page'];
			
			$this->page = isset($this->config['page'][$page])?$this->config['page'][$page]:$this->config['page404'];
			
			//Grab lis of all "ACTIVE" pages so we acn addit to menu
			$data['menu'] = $this->config['page'];
			//Site information
			$data['site'] = $this->config['site'];
			//General <link> and <Script> tags as needed
			$data['frontEndFiles'] = $this->getAllFrontEndFiles($page);
			//Page information for title etc
			$data['page'] = $this->page;
			
			$this->includeFiles($data);
			
		}
		/**
		 * Get CSS and JS global files and specifically for current page
		 */
		private function getAllFrontEndFiles($page){
			$arr = isset($this->config['css'][$page])?$this->config['css'][$page]:array();
			$arr1 = isset($this->config['css']['all']) && is_array($this->config['css']['all'])?$this->config['css']['all']:array();
			$css = array_merge($arr1,$arr);
			$arr = isset($this->config['js'][$page])?$this->config['js'][$page]:array();
			$arr1 = isset($this->config['js']['all']) && is_array($this->config['js']['all'])?$this->config['js']['all']:array();
			$js = array_merge($arr1,$arr);
			
			$content = '';
			foreach($css as $key=>$value) {
				foreach($value as $key1=>$value1){
					$content .= '<link rel="stylesheet" media="'.$key.'" href="'.$this->dirTemplate.'/'.$this->template.'/'.$value1.'"/>';
				}
			}
			foreach($js as $key=>$value) {
				$content .= '<script type="text/javascript" src="'.$value.'"></script>';
			}
			return $content;
		}
		/**
		 * Include files for templates
		 */
		private function includeFiles($data){
			$path = $this->dirTemplate.'/'.$this->template.'/';
			
			//If no page data file then show Page not found error and exit
			if(!is_file($path.'/'.$this->page['file'])){
				$this->pageNotFound();
			}
			
			//Include Header
			require_once($path.$this->config['header']);
			//Include page
			require_once($path.'/'.$this->page['file']);
			//Include Footer
			require_once($path.$this->config['footer']);
		}
	}